package presstrack
{
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	import org.presstrack.ContactClass;
	import org.presstrack.service.CrudService;
	
	[RemoteClass(alias="presstrack.CustomerClass")]
	
	[Bindable]
	public class CustomerClass extends UserClass
	{
		public var arCustomerEmail:ArrayCollection;
		public var arCustomerPhone:ArrayCollection;
		public var arCustomerAddress:ArrayCollection;
		public var arCustomerNote:ArrayCollection;
  		
  		public var sale:ArrayCollection;
  		public var ar_debt:ArrayCollection;
  		public var ar_num:ArrayCollection;
  		
  		public var arPayment:ArrayCollection; 		
  		
		public function CustomerClass(user:UserClass = null) 
		{
			if(user != null)
			{
				super.copyFrom(user);
				loadPayment(new Date().getFullYear());
			}			
			
			arCustomerEmail		= new ArrayCollection();
			arCustomerPhone		= new ArrayCollection();
			arCustomerAddress	= new ArrayCollection();
			arCustomerNote		= new ArrayCollection();
			
			for each (var ob:Object in arContact) 
			{
				var contact:ContactClass = new ContactClass();
				contact.setFromObject(ob);
				if (contact.type == 1)
				{
					arCustomerEmail.addItem(contact);
				}
				else if (contact.type == 2)
				{
					arCustomerPhone.addItem(contact);
				}
				else if (contact.type == 3)
				{
					arCustomerAddress.addItem(contact);
				}
				else if (contact.type == 4)
				{
					arCustomerNote.addItem(contact);
				}
			}
		}
		
		public function addContact(info			:String, 
        						   type			:int, 
        						   resFunc		:Function	= null, 
        						   faultFunc	:Function	= null):void 
        {
			CrudService.crud.srv.call("PressTrack.AddUserContact", new Responder(onResult), iUserId, type, info);
			
			function onResult(id:String):void 
			{
				if (Number(id))
				{
					var contact:ContactClass	= new ContactClass();
					contact.idcontact			= Number(id);
					contact.body				= info;
					contact.type				= type;
					
					switch(contact.type) {
						case 1 :
							arCustomerEmail.addItem(contact); 
						break;
						
						case 2 : 
							arCustomerPhone.addItem(contact); 
						break;
						
						case 3 : 
							arCustomerAddress.addItem(contact); 
						break;
						
						case 4 : 
							arCustomerNote.addItem(contact); 
						break;
					}
					
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else
					faultFunc(id);
			}
			
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}
		}
		
		public function deleteContact(idcontact	:int, 
									  resFunc	:Function	= null, 
									  faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.RemoveUserContact", new Responder(onResult, onFault), idcontact);	
			
			function onResult(res:String):void 
			{
				if (res == 'successful') 
				{
					if (resFunc != null)
					{
						resFunc();
					}
				}
				else
				{	
					faultFunc(res);
				}			
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}
		}
		
		public function loadPayment(year		:int,
									resFunc		:Function	= null, 
									faultFunc	:Function	= null):void
		{
			CrudService.crud.srv.call("PressTrack.GetPayments", new Responder(onResult, onFault), iUserId, year);
			
			function onResult(ar:Array):void 
			{
				arPayment = new ArrayCollection(ar);	
				arPayment.refresh();	
				if (resFunc != null)
				{
					resFunc();
				}	
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}	
		}
		/*public function setFromObject(ob:Object):void {
			if (ob.hasOwnProperty("count"))
				count = Number(ob.count);
				
			if (ob.hasOwnProperty("total"))
				total = Number(ob.total);
				
			if (ob.hasOwnProperty("debt"))
				debt = Number(ob.debt);
				
			if (ob.hasOwnProperty("sale"))
				sale = new ArrayCollection(ob.sale);
			
			if (ob.hasOwnProperty("ar_debt")) {	
				ar_debt = new ArrayCollection(ob.ar_debt);
				for each(var d:Object in ar_debt)
					debt += Number(d);
			}
				
			if (ob.hasOwnProperty("ar_num"))	
				ar_num = new ArrayCollection(ob.ar_num);
				
			if (ob.hasOwnProperty("statistics"))	
				statistics = ob.statistics;
		}
		
		public function getStatistics( year:String, resFunc:Function = null, faultFunc:Function = null  ):void {
			CrudService.crud.srv.call( "PressTrack.getCustStatistics", new Responder( onResult, onFault ), year, iduser);
			
			function onResult(res:Object) : void {
				statistics = res;
				CrudService.arCustomer.refresh();
				if (resFunc != null) resFunc();			
			}	
			
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}
		}
		
		*/		
	}
}